﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;

namespace CodeIn.Modules.Graph.Modules.Ui.Flow.View {
	public class LinkVisual : DrawingVisual {
		private NodeVisual n1;
		private NodeVisual n2;
		private ScaleTransform scaleTransform;
		private RotateTransform rotateTransform;
		private TranslateTransform translateTransform;

		public LinkVisual(NodeVisual n1, NodeVisual n2) {
			this.n1 = n1;
			this.n2 = n2;

			DrawingContext dc = RenderOpen();
			dc.DrawDrawing(FlowResources.LinkBaseDrawing);
			dc.Close();

			scaleTransform = new ScaleTransform();
			rotateTransform = new RotateTransform();
			translateTransform = new TranslateTransform();

			TransformGroup tg = new TransformGroup();
			tg.Children.Add(scaleTransform);
			tg.Children.Add(rotateTransform);
			tg.Children.Add(translateTransform);
			Transform = tg;
		}

		public void UpdateVisual() {
			double dx = n2.P.X - n1.P.X;
			double dy = n2.P.Y - n1.P.Y;

			double angle = Math.Atan2(dy, dx);
			double dist = Math.Sqrt(dx*dx + dy*dy);

			scaleTransform.ScaleX = dist / 100;
			rotateTransform.Angle = angle * 180 / Math.PI;
			translateTransform.X = n1.P.X;
			translateTransform.Y = n1.P.Y;
		}
	}
}
